<html>
<head>
    <meta charset="utf-8"/>
    <title>Zadatak 1 - profil studenta</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="content">

    <a href="index.php">Nazad</a><br>
    <?php

    if(isset($_GET['ime']) && isset($_GET['prezime'])){
        require_once "Konekcija.php";
        $ime = $_GET['ime'];
        $prezime = $_GET['prezime'];
        $indeks = $_GET['indeks'];

        $sqlUpdate = "update student set ime='".$ime."', prezime='".$prezime."' where indeks='".$indeks."'";
        if(!$queryUpdate = $mysqli->query($sqlUpdate))
            echo "Greska pri izvodjenju upita" . mysql_error();
        else
            echo "<script>alert('Podaci su uspešno izmenjeni');</script>";
    }

    if(isset($_POST['predmet']) && isset($_POST['ocena']))
    {
        require_once "Konekcija.php";

        $indeks = $_GET['indeks'];
        $predmet = $_POST['predmet'];
        $ocena = $_POST['ocena'];
        $sqlOcena = "insert into ispit (indeks, id, ocena) values ('".$indeks."',".$predmet.",".$ocena.")";
        if(!$queryOcena = $mysqli->query($sqlOcena))
            echo "Greska pri izvodjenju upita" . mysql_error();
        else
            echo "<script>alert('Ocena je uneta');</script>";
    }

    if (isset($_POST['ispit']))
    {
        require_once "Konekcija.php";

        $indeks = $_GET['indeks'];
        $id = $_POST['ispit'];
        $sqlDel = "delete from ispit where indeks = '".$indeks."' and id = " .$id;
        if(!$queryDel = $mysqli->query($sqlDel))
            echo "Greska pri izvodjenju upita" . mysql_error();
        else
            echo "<script>alert('Ispit je obrisan');</script>";
    }


    if (isset ($_GET['indeks'])) {
        $indeks = $_GET['indeks'];

        require_once "Konekcija.php";
        require_once 'Student.php';
        require_once "Ispit.php";
        require_once "Predmet.php";

        $sql1 = "select * from student where student.indeks = '".$indeks."'";
        if(!$query1 = $mysqli->query($sql1))
            echo "Greska pri izvodjenju upita" . mysql_error();


        $stud = $query1->fetch_object();
        echo "<h1>";
        echo $stud->ime . " " . $stud->prezime;
        echo "</h1>";
        echo "<p><b>Indeks:</b> ";
        echo $stud->indeks;
        echo "</p>";
        echo "<p> <b>Smer:</b> " . $stud->smer . "</p>";
        echo "<p> <b>Datum rodjenja:</b> " . $stud->datum_rodjenja . "</p>";
        ?>
        <form method="post" action="izmeniStudenta.php">
            <input type="hidden" name="indeks" value="<?php echo $indeks;?>">
            <input type="submit" value="Izmeni informacije o studentu">
        </form>
        <div id="levo">
            <?php
            $sql2 = "select * from ispit join predmet on ispit.id = predmet.id where ispit.indeks = '".$indeks."'";
            if(!$query2 = $mysqli->query($sql2))
                echo "Greska pri izvodjenju upita" . mysql_error();

            echo "<h2>Ocene</h2>";
            $numRows = $query2->num_rows;
            if ($numRows > 0){
                ?>
                <form method="post" action="studentProfil.php?indeks=<?php echo $indeks; ?>">
                    <ul id="listaOcena">
                        <?php
                        while($row = $query2->fetch_object())
                        {
                            echo '<li><input type="radio" name="ispit" value="'. $row->id .'"/>';
                            echo $row->naziv . " " . $row->ocena . "</li>\n";
                        }
                        ?>
                    </ul>
                    <input type="submit" value="Obriši ispit" />
                </form>

            <?php
            }
            else {
                echo "<p>Student nema polozene ispite</p>";
            }

            ?>
        </div>
        <form id="desno" method="post" action="studentProfil.php?indeks=<?php echo $indeks; ?>">
            <h2>Dodavanje ocena</h2>

            <label>Ocena:</label><input type="number" name="ocena" max="10" min="6" required>
            <label>Predmet:</label><select name="predmet">
                <?php
                $sqlPredmeti = "select * from predmet";
                if(!$queryPredmeti = $mysqli->query($sqlPredmeti))
                    echo "Greska pri izvodjenju upita" . mysql_error();
                while($row = $queryPredmeti->fetch_object())
                {
                    echo '<option value="'. $row->id .'">';
                    echo $row->naziv . "</option>\n";
                }
                ?>
            </select><br>
            <input type="submit" value="Pošalji ocenu">
        </form>
    <?php

    }
    ?>

</div>
</body>
</html>

